import request from 'request.js'
const global = getApp().globalData
var tokenReq = require('../jsons/reqJson/tokenReq.js');

// 认证得到Token
//X-Auth-Token
//token.project.id
export function authToken(params) {
  var data = tokenReq.reqTokenJson;
  data.auth.identity.password.user.name = params.iamAccount
  data.auth.identity.password.user.password = params.iamPassword
  data.auth.identity.password.user.domain.name = params.account
  console.log("auth_data", data)
  return request({
    url: `${global.IAMEndpoint}/v3/auth/tokens`,
    method: 'post',
    data: data
  })
}
export function init(params, func) {
  getAppSpace({
    "project_id": params.project_id
  }).then(res => {
    var app_name = global.app_name;
    var applications = res.applications;
    for (var item of applications) {
      if (item.app_name == app_name) {
        global.app_id = item.app_id
        break;
      }
    }
    if (global.app_id) {
      console.log('app_id', global.app_id);
      // getProducts({ 'app_id': global.app_id }).then(res => {
      //   var products = res.products;
      //   global.products = res.products;
      //   func(products)
      // })
      func(true)
    } else {
      addAppSpace({
        'project_id': global.project_id || wx.getStorageSync('project_id')
      }).then(res => {
        global.app_id = res.app_id
        console.log('app_id', global.app_id);
        // addProduct().then(res => {
        //   func(true)
        // })
        func(true)
      })
    }
  });

}

function getProject(params) {
  return request({
    url: `${global.IAMEndpoint}/v3/auth/projects`,
    method: 'get'
  })
}

function getAppSpace(params) {
  params = params || {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/apps`,
    method: 'get'
  })
}

function addAppSpace(params) {
  params = params || {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/apps`,
    method: 'post',
    data: {
      "app_name": params.app_name || global.app_name
    }
  })
}
export function getProducts(params) {
  params = params || {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  let app_id = params.app_id || global.app_id
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/products?app_id=${app_id}`,
    method: 'get'
  })
}

function getProduct(params) {
  params = params || {}
  var product = {}
  request({
    url: `${global.IOTDAEndpoint}/v5/iot/${global.project_id}/products?app_id=${global.app_id}`,
    method: 'get'
  }).then(res => {
    var product_name = params.product_name;
    var jsonData = res;
    var products = jsonData.products;
    for (let item of products) {
      if (item.name == product_name) {
        product = item
        break;
      }
    }
    console.log('product_id', product.id);
    return product

  });
}

export function addProduct(params) {
  params = params || {}
  let project_id = params.project_id
  var data = params.productCreateJson
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/products`,
    method: 'post',
    data: data
  })
}
//name openid content
export function saveMyProduct(params) {
  console.log('saveMyProduct',params);
  return request({
    url: `${global.MyIoTDomain}/api/bearpi/SaveProfile`,
    method: 'post',
    data: params
  })
}
export function getProfile(params) {
  return request({
    url: `${global.MyIoTDomain}/api/bearpi/getProfile?openid=${params.openid}&productName=${params.product_name}`,
    method: 'post',
    data: params
  })
}
//name openid content
export function loadMyProducts(params) {
  return request({
    url: `${global.MyIoTDomain}/api/bearpi/loadMyProducts?openid=${params.openid}`,
    method: 'get',
    data: params
  })
}

function getDeviceById(params) {
  params = params || {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  let device_id = params.device_id || global.device_id
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/devices/${device_id}`,
    method: 'get'
  })
}
export function delDevice(params) {
  params = params || {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  let device_id = params.device_id
 console.log(`${global.IOTDAEndpoint}/v5/iot/${project_id}/devices/${device_id}`);
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/devices/${device_id}`,
    method: 'delete'
  })

}
// 资源空间下所有产品的设备
export function getAppDevices(params) {
  //https://iotda.cn-north-4.myhuaweicloud.com/v5/iot/055be9cfdc0025002f56c01b641202a8/devices?app_id=s__HdJSX7r5oy45ApqC77VyKu1Ya
  if (!params) params = {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  let app_id = params.app_id || global.app_id
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/devices?app_id=${app_id}`,
    method: 'get'
  })
}
export function addDevice(params) {
  var data = params.deviceCreateJson
  let project_id = params.project_id || wx.getStorageSync('project_id')
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/devices`,
    method: 'post',
    data: data
  })
}
//查询指定产品下的设备
export function getProductDevices(params) {
  if (!params) params = {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  let product_id = params.product_id
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/devices?product_id=${product_id}`,
    method: 'get'
  })
}

//影子属性
export function getDeviceProperties(params) {
  if (!params) params = {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  let device_id = params.device_id
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/devices/${device_id}/shadow`,
    method: 'get'
  })
}
//执行命令
export function sendCmd(params) {
  if (!params) params = {}
  let project_id = params.project_id || global.project_id || wx.getStorageSync('project_id')
  let device_id = params.device_id
  var data = {
    "service_id": params.service_id,
    "command_name": params.command_name,
    "paras": params.paras
  }
  return request({
    url: `${global.IOTDAEndpoint}/v5/iot/${project_id}/devices/${device_id}/commands`,
    method: 'post',
    data: data
  })
}